#!/usr/bin/perl -w
use strict;
use SADI::Service::Core;
use lib ".";
use SOAP::Lite;
use HTTP::Cookies;

my $service = SADI::Service::Core->new(
    ServiceName => "getMolecularInteractions",
    ServiceType => "http://sadiframework.org/RESOURCES/___NoIdea___",
    InputClass => "http://purl.oclc.org/SADI/LSRN/UniProt_Record",
    OutputClass => "http://sadiframework.org/ontologies/service_objects.owl#getMolecularInteractions_Output",
    Description => "gets the BIND interaction ids for a given UniProt protein",
    UniqueIdentifier => "urn:lsid:sadiframework.org:serviceinstances:sadiframework.net,getMolecularInteractions",
    Authority => "illuminae.com",
    ServiceURI => "http://sadiframework.org/services/getMolecularInteractions",
    URL => "http://sadiframework.org/services/getMolecularInteractions",
    #_default_request_method => "POST",
                                    
    );

$service->sendInterfaceOnGET ;  # if GET then send interface and exit
$service->Prepare() || die "somehow the input data was improperly formed\n";

my @inputs = $service->getInputNodes();

&process_data($service, \@inputs);

$service->Respond();

sub process_data {
    my ($service, $inputs) = @_;
    
    my @inputs = @$inputs;
    my $SOAP = SOAP::Lite
	    -> service('http://soap.bind.ca/bind.wsdl')
	    -> proxy('http://soap.bind.ca/services/BINDSOAP',
		    cookie_jar => HTTP::Cookies->new(ignore_discard => 1));

    foreach my $input(@inputs){
	my $UniProt = $input->getURI;
	my $value = $input->getLocalValue;

	my $resultBean = $SOAP->idSearch($value,"uniprot","idlist");
	my @results = split "\n", $resultBean->{records};

	foreach my $bind_id(@results){
	    my $bind_node = RDF::Core::Resource->new("http://www.bind.ca/unresolvable/BIND_ID/$bind_id");
	    $service->addOutputData(node => $UniProt,      # UniProt inBindInteraction BIND_ID
				    value => $bind_node,
				    predicate => "http://es-01.chibi.ubc.ca/~benv/predicates.owl#inBINDInteraction",
				    typed_as_output => "true",
				    );
	    my $bind_type_node = "http://ontology.dumontierlab.com/BIND";
	    my $typepredicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
            $service->addOutputData(node => $bind_node, value => $bind_type_node, predicate => $typepredicate);
	    my $attrib = "The Biomolecular Interaction Network Database and related tools 2005 update. Nucleic Acids Res. 2005 Jan 1;33(Database issue):D418-24";
	    my $source = "http://purl.org/dc/elements/1.1/source";
            $service->addOutputData(node => $bind_node, value => $attrib, predicate => $source);


	    my $resultBean = $SOAP->idSearch($bind_id,"bindid","go");
	    my @go_list = split "\n", $resultBean->{records};
	    shift @go_list;  # get rid of header line
	    foreach my $gi_line(@go_list){
		$gi_line =~ /[^,]+,([^,]+),/;   # bind_id, ((GI)) , GO_ID, GO_TERM
		my $gi = $1;
		my $ncbi_uri = "http://view.ncbi.nlm.nih.gov/protein/$gi";
		
		$service->addOutputData(node => $bind_node,   # BIND_ID  hasInteractingParticipant  GI
					value => $ncbi_uri,
					predicate => "http://es-01.chibi.ubc.ca/~benv/predicates.owl#hasInteractingParticipant"
					);
		$service->addOutputData(node => $UniProt,   # UniProt  hasMolecularInteractionWith  GI
					value => $ncbi_uri,
					predicate => "http://es-01.chibi.ubc.ca/~benv/predicates.owl#hasMolecularInteractionWith"
					);
		my $GI_type = "http://purl.oclc.org/SADI/LSRN/GI";
		$service->addOutputData(node => $ncbi_uri, value => $GI_type, predicate => $typepredicate);
	    }
	}
    }
}
